package com.fenghuajueli.libbasecoreui.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.adapter.CustomMenuAdapter;
import com.fenghuajueli.libbasecoreui.entity.MenuEntity;
import com.fenghuajueli.libbasecoreui.listener.CustomMenuListener;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.utils.CustomItemDecoration;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装视频编辑界面菜单控件
 */
public class CustomMenuView extends RelativeLayout {

    public static final int MENU_TYPE_MAIN = 1001;
    public static final int MENU_TYPE_SECOND = 1002;
    public static final int MENU_TYPE_THIRD = 1003;
    /**
     * 主菜单的recyclerview
     */
    private RecyclerView mainMenuView;
    /**
     * 显示子菜单
     */
    private RecyclerView childMenuView;
    //第三级菜单
    private RecyclerView thirdMenuView;

    private int menuHeight;
    private Context mContext;
    private List<MenuEntity> menuEntityList;
    private List<MenuEntity> childMenuList;
    private List<MenuEntity> thirdMenuList;
    private ImageView ivBackView;
    private ImageView thirdBackView;
    private CustomMenuAdapter mainMenuAdapter;
    private CustomMenuAdapter childMenuAdapter;
    private CustomMenuAdapter thirdMenuAdapter;
    private View childBgView;
    private View thirdBgView;
    private CustomMenuListener customMenuListener;
    private View fadeSettingView;
    private OnClickListener onCancelClickListener;
    private OnClickListener onThirdBackClickListener;
    private int expendMenuType = MENU_TYPE_MAIN;


    public CustomMenuView(Context context) {
        this(context, null);
    }

    public CustomMenuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }


    private void initView() {
        menuHeight = ConvertUtils.dp2px(60);
        setBackgroundColor(Color.BLACK);
        mainMenuView = new RecyclerView(mContext);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mainMenuView.setLayoutManager(linearLayoutManager);
        mainMenuView.setLayoutParams(layoutParams);
        mainMenuView.addItemDecoration(new CustomItemDecoration(mContext, ConvertUtils.dp2px(8), 0));
        addView(mainMenuView);

        childBgView = new View(mContext);
        childBgView.setBackgroundColor(Color.BLACK);
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        childBgView.setLayoutParams(layoutParams);
        childBgView.setClickable(true);
        addView(childBgView);

        childMenuView = new RecyclerView(mContext);
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.leftMargin = ConvertUtils.dp2px(60);
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        childMenuView.setLayoutManager(linearLayoutManager);
        childMenuView.setLayoutParams(layoutParams);
        childMenuView.addItemDecoration(new CustomItemDecoration(mContext, ConvertUtils.dp2px(8)));
        addView(childMenuView);

        ivBackView = new ImageView(mContext);
        LayoutParams backLayoutParams = new LayoutParams(ConvertUtils.dp2px(26), ConvertUtils.dp2px(37));
        backLayoutParams.leftMargin = ConvertUtils.dp2px(24);
        backLayoutParams.topMargin = ConvertUtils.dp2px(12);
        ivBackView.setImageResource(R.mipmap.ic_editor_return);
        ivBackView.setLayoutParams(backLayoutParams);
        addView(ivBackView);


        thirdBgView = new View(mContext);
        thirdBgView.setBackgroundColor(Color.BLACK);
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        thirdBgView.setLayoutParams(layoutParams);
        thirdBgView.setClickable(true);
        addView(thirdBgView);

        thirdMenuView = new RecyclerView(mContext);
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.leftMargin = ConvertUtils.dp2px(60);
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        thirdMenuView.setLayoutManager(linearLayoutManager);
        thirdMenuView.setLayoutParams(layoutParams);
        thirdMenuView.addItemDecoration(new CustomItemDecoration(mContext, ConvertUtils.dp2px(8)));
        addView(thirdMenuView);

        thirdBackView = new ImageView(mContext);
        backLayoutParams = new LayoutParams(ConvertUtils.dp2px(26), ConvertUtils.dp2px(37));
        backLayoutParams.leftMargin = ConvertUtils.dp2px(24);
        backLayoutParams.topMargin = ConvertUtils.dp2px(12);
        thirdBackView.setImageResource(R.mipmap.icon_return_02);
        thirdBackView.setLayoutParams(backLayoutParams);
        addView(thirdBackView);

        hideChildMenu();
        hideThirdMenu();

        childMenuList = new ArrayList<>();
        menuEntityList = new ArrayList<>();
        thirdMenuList = new ArrayList<>();
        mainMenuAdapter = new CustomMenuAdapter(mContext, menuEntityList);
        childMenuAdapter = new CustomMenuAdapter(mContext, childMenuList);
        thirdMenuAdapter = new CustomMenuAdapter(mContext, thirdMenuList);

        mainMenuView.setAdapter(mainMenuAdapter);
        childMenuView.setAdapter(childMenuAdapter);
        thirdMenuView.setAdapter(thirdMenuAdapter);
        ivBackView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                hideChildMenu();
                if (onCancelClickListener != null) {
                    onCancelClickListener.onClick(view);
                }
            }
        });
        thirdBackView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                hideThirdMenu();
                if (onThirdBackClickListener != null) {
                    onThirdBackClickListener.onClick(view);
                }
            }
        });
        mainMenuAdapter.setOnListItemClickListener(new OnListClickListener<MenuEntity>() {
            @Override
            public void itemClick(int position, MenuEntity data) {
                if (QuickClickUtils.isFastClick()) {
                    return;
                }
                if (data.getChildMenuList() != null && data.getChildMenuList().size() > 0) {
                    expendMenuType = MENU_TYPE_SECOND;
                    //还有子菜单，展开
                    refreshChildMenu(data.getName(), data.getChildMenuList());
                    showChildMenu();
                }
                if (customMenuListener != null) {
                    customMenuListener.primaryMenuClick(position, data);
                }
            }
        });
        childMenuAdapter.setOnListItemClickListener(new OnListClickListener<MenuEntity>() {
            @Override
            public void itemClick(int position, MenuEntity data) {
                if (QuickClickUtils.isFastClick()) {
                    return;
                }
                if (customMenuListener != null) {
                    if (data.hasThirdMenu()) {
                        expendMenuType = MENU_TYPE_THIRD;
                        refreshThirdMenu(data.getName(), childMenuAdapter.getFatherName(), data.getThirdMenuList());
                        showThirdMenu();
                    } else {
                        customMenuListener.childMenuClick(position, data, childMenuAdapter.getFatherName());
                    }
                }
            }
        });
        thirdMenuAdapter.setOnListItemClickListener(new OnListClickListener<MenuEntity>() {
            @Override
            public void itemClick(int position, MenuEntity data) {
                if (QuickClickUtils.isFastClick()) {
                    return;
                }
                if (customMenuListener != null) {
                    LogUtils.d("点击了第三级菜单：" + childMenuAdapter.getMainMenuName()
                            + "---->" + thirdMenuAdapter.getFatherName() + "---->" + data.getName());
                    customMenuListener.thirdMenuClick(position, data, thirdMenuAdapter.getFatherName(), childMenuAdapter.getMainMenuName());
                }
            }
        });
    }

    public void setOnCancelClickListener(OnClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }


    public void setOnThirdBackClickListener(OnClickListener onThirdBackClickListener) {
        this.onThirdBackClickListener = onThirdBackClickListener;
    }

    /**
     * 设置展示淡入淡出按钮
     */
    public void showFadeButton() {
        fadeSettingView.setVisibility(VISIBLE);
        LayoutParams childParams = (LayoutParams) childMenuView.getLayoutParams();
        childParams.rightMargin = ConvertUtils.dp2px(83);
        childMenuView.setLayoutParams(childParams);
    }


    public void setCustomMenuListener(CustomMenuListener customMenuListener) {
        this.customMenuListener = customMenuListener;
    }

    public void setMenuData(List<MenuEntity> menuEntityList) {
        this.menuEntityList = menuEntityList;
        if (mainMenuAdapter != null) {
            mainMenuAdapter.refreshData(menuEntityList);
        }
    }

    private void refreshChildMenu(String fatherName, List<MenuEntity> childMenuList) {
        if (childMenuAdapter != null) {
            childMenuAdapter.setFatherName(fatherName);
            childMenuAdapter.setMainMenuName(fatherName);
            childMenuAdapter.refreshData(childMenuList);
        }
    }

    private void refreshThirdMenu(String fatherName, String mainName, List<MenuEntity> childMenuList) {
        if (thirdMenuAdapter != null) {
            thirdMenuAdapter.setFatherName(fatherName);
            thirdMenuAdapter.setMainMenuName(mainName);
            thirdMenuAdapter.refreshData(childMenuList);
        }
    }

    public void hideChildMenu() {
        childBgView.setVisibility(GONE);
        ivBackView.setVisibility(GONE);
        childMenuView.setVisibility(GONE);
        mainMenuView.setVisibility(VISIBLE);
        hideThirdMenu();
    }


    public void showChildMenu() {
        childBgView.setVisibility(VISIBLE);
        ivBackView.setVisibility(VISIBLE);
        childMenuView.setVisibility(VISIBLE);
        mainMenuView.setVisibility(GONE);
    }


    public void hideThirdMenu() {
        thirdBgView.setVisibility(GONE);
        thirdBackView.setVisibility(GONE);
        thirdMenuView.setVisibility(GONE);
    }


    public void showThirdMenu() {
        thirdBgView.setVisibility(VISIBLE);
        thirdBackView.setVisibility(VISIBLE);
        thirdMenuView.setVisibility(VISIBLE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, menuHeight);
    }

    /**
     * 设置主菜单删除按钮的状态
     *
     * @param isCanUse
     */
    public void setMainMenuStatus(int position, boolean isCanUse) {
        if (mainMenuAdapter != null) {
            if (position >= 0 && position < menuEntityList.size()) {
                menuEntityList.get(position).setAvailable(isCanUse);
                mainMenuAdapter.notifyDataSetChanged();
            }
        }
    }


    /**
     * 设置子菜单删除按钮的状态
     *
     * @param isCanUse
     */
    public void setChildMenuStatus(int position, boolean isCanUse) {
        if (childMenuAdapter != null) {
            if (position >= 0 && position < childMenuList.size()) {
                childMenuList.get(position).setAvailable(isCanUse);
                childMenuAdapter.notifyDataSetChanged();
            }
        }
    }


    public void showChildMenu(String name) {
        if (menuEntityList != null) {
            for (MenuEntity menuEntity : menuEntityList) {
                if (menuEntity.getName().equals(name)) {
                    refreshChildMenu(name, menuEntity.getChildMenuList());
                    showChildMenu();
                    break;
                }
            }
        }
    }

    /**
     * 展示第三级菜单
     */
    public void showThirdMenuView(String name) {
        if (menuEntityList != null) {
            for (MenuEntity menuEntity : menuEntityList) {
                if (menuEntity.getChildMenuList() != null) {
                    for (MenuEntity entity : menuEntity.getChildMenuList()) {
                        if (entity.getName().equals(name)) {
                            refreshThirdMenu(name, menuEntity.getName(), entity.getThirdMenuList());
                            showThirdMenu();
                            break;
                        }
                    }
                }
            }
        }
    }

    public void setBackIcon(int resId, int width, int height) {
        LayoutParams layoutParams = (LayoutParams) ivBackView.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
        ivBackView.setLayoutParams(layoutParams);
        ivBackView.setImageResource(resId);
    }

    /**
     * 设置返回按钮的drawable
     *
     * @param drawable
     * @param width
     * @param height
     */
    public void setBackIconDrawable(Drawable drawable, int width, int height) {
        LayoutParams layoutParams = (LayoutParams) ivBackView.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
        ivBackView.setLayoutParams(layoutParams);
        ivBackView.setImageDrawable(drawable);
    }
}
